package jdbcutil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jdbc.ConexaoJdbc;
import jdbc.DmlJdbc;
import jdbc.StatementNomeado;

public class MoedaDao {
	private ConexaoJdbc conexao;
	
	public MoedaDao() {
		conexao = new ConexaoJdbc(DmlJdbc.HOSTNAME, DmlJdbc.PORTNUMBER, DmlJdbc.DATABASENAME, DmlJdbc.USERNAME, DmlJdbc.PASSWORD, ConexaoJdbc.MOTOR_MYSQL);
	}
	
	public List<Moeda> findAll(){
		List<Moeda> items = new ArrayList<Moeda>();
    	ResultSet rs = null;
		
		try {
			conexao.criarPreparedStatment(DmlJdbc.QUERY_1);
			conexao.executarQueryComPreparedStatement();
			rs = conexao.getResultSet();

			while (rs.next()) {
				Moeda bean = new Moeda();
				bean.setId(rs.getObject("id").toString());
				bean.setAbrev(rs.getObject("abrev").toString());
				bean.setMoeda(rs.getObject("moeda").toString());
				items.add(bean);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			conexao.fecharConexao();
		}
		return items;
	}
	
	public Moeda findByID(String id){
		Moeda bean = new Moeda();
    	ResultSet rs = null;		
		try {
		String s = "select * from cliente where id = :idcliente and nome = :nome";
			StatementNomeado pstmt = new StatementNomeado(conexao.getConnection(), DmlJdbc.QUERY_1);
			pstmt.setString("idMoeda", id);
			rs = pstmt.executeQuery();

			if (rs.next()) {
				bean.setId(rs.getObject("id").toString());
				bean.setAbrev(rs.getObject("abrev").toString());
				bean.setMoeda(rs.getObject("moeda").toString());			
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			conexao.fecharConexao();
		}
		return bean;
	}

}
